// pages/supplier-home/supplier-home.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */ 
  data: {
    active: 0,
    list: [],
    list1: [],
    list2:[],
    params: { page: 1 },
    params1: { page: 1 },
    params2: { page: 1 },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    const name = wx.getStorageSync('supplierName')
    wx.setNavigationBarTitle({
      title: name+'供应商',
    })


  },
  loadMore(e) {
    const userId = wx.getStorageSync('userId')

    
    let { params } = this.data
    this.setData({
      'params.page': params.page + 1
    }, () => {
      app.reqFetch.selecSupplierOd({
        ...this.data.params,
        supplierId: userId
      }, (res) => {
        const { list = [] } = res
  
        this.setData({
          list: this.data.list.concat(list)
        })
      })
    })
  },
  loadMore1(e) {
    const userId = wx.getStorageSync('userId')
    let { params1,  } = this.data
    this.setData({
      'params1.page1': params.page + 1
    }, () => {
      app.reqFetch.selecSupplierMateriOrder({
        ...this.data.params1,
        supplierId: userId
      }, (res) => {
        const { list = [] } = res
        
        this.setData({
          list1: this.data.list1.concat(list)
        })
      })
    })
  },
  loadMore2(e) {
    const userId = wx.getStorageSync('userId')
    let { params2 } = this.data
    this.setData({
      'params2.page': params2.page + 1
    }, () => {
      app.reqFetch.selecSupplierSmallOrder({
        ...this.data.params2,
        supplierId: userId
      }, (res) => {
        const { list = [] } = res
        this.setData({
          list2: this.data.list2.concat(list)
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrder()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  getOrder(){
      
    const userId  = wx.getStorageSync('userId')
    app.reqFetch.selecSupplierOd({
      supplierId: userId
    }, (res) => {
      const { list = [], pending}=res
      this.setData({ list, pending})
    })
    app.reqFetch.selecSupplierMateriOrder({
      supplierId: userId
    }, (res) => {
      const { list = [], pending} = res
      this.setData({ list1: list, pending1: pending })
    })
    app.reqFetch.selecSupplierSmallOrder({
      supplierId: userId
    }, (res) => {
      const { list = [], pending} = res
      this.setData({ list2: list, pending2: pending })
    })
  },

  toDetails(e){
    const { stockOrderId, status, auxiliaryMaterialsId, smallCanteenPurchaseId, mealTime } = e.currentTarget.dataset.record

    const type = e.currentTarget.dataset.type
    if (status === 2 || status ===3){
      wx.navigateTo({
        url: '/pages/supplier-details/supplier-details?data=' + JSON.stringify({ audit: true, stockOrderId, auxiliaryMaterialsId, smallCanteenPurchaseId, mealTime, type }),
      })
    }else{
      wx.navigateTo({
        url: '/pages/supplier-details/supplier-details?data=' + JSON.stringify({ readonly: true, stockOrderId, auxiliaryMaterialsId, smallCanteenPurchaseId, mealTime, type }),
      })
    }
  },
  onChange(event) {
    if (event.detail === 1) {
      wx.redirectTo({
        url: '/pages/mine/mine',
      })
    }
  },
})